from flask import request, render_template, Blueprint

from blog.post.models import Post

main = Blueprint('main', __name__)


@main.route('/index')
@main.route('/')
def index():
    """网站主页面"""
    # http://127.0.0.1:5000/?page=3
    page = request.args.get('page', default=1, type=int)
    # 排序-逆序
    posts = Post.query.order_by(Post.created.desc()).paginate(page=page, per_page=5)  # 每頁五個
    return render_template('index.html', posts=posts)


@main.route('/about')
def about():
    """关于该网站的一些简短信息"""
    return render_template('about.html', title='About')


@main.app_errorhandler(404)
def handle_404(error):
    return render_template('404.html'), 404  # status code


@main.app_errorhandler(403)
def handle_403(error):
    return render_template('403.html'), 403


@main.app_errorhandler(500)
def handle_500(error):
    return render_template('500.html'), 500
